package com.ecnu.web.controller.project;


import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectPlan;
import com.ecnu.system.domain.ProjectResult;
import com.ecnu.system.domain.ProjectStage;
import com.ecnu.system.domain.ProjectStageMission;
import com.ecnu.system.domain.dto.ProjectResultDTO;
import com.ecnu.system.domain.dto.StageMissionDTO;
import com.ecnu.system.domain.vo.ProjectStageVO;
import com.ecnu.system.service.IProjectPlanService;
import com.ecnu.system.service.IProjectStageMissionService;
import com.ecnu.system.service.IProjectStageService;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ecnu_活动_阶段表 前端控制器
 * </p>
 *
 * @author zdn
 * @since 2025-03-28
 */
@Api(tags = "活动阶段管理")
@RestController
@RequestMapping("/project/stage")
public class ProjectStageController extends BaseController {
    @Autowired
    private IProjectStageService projectStageService;
    @Autowired
    private IProjectStageMissionService stageMissionService;
    @Autowired
    private IProjectPlanService planService;

    /**
     * 阶段模块
     */
//    @ApiOperation(value = "阶段列表")
//    @GetMapping("/list")
//    public AjaxResult list(String proId) {
//        return projectStageService.listByProId(proId);
//    }
//
//
//    @ApiOperation(value = "删除")
//    @GetMapping("/del")
//    public AjaxResult del(String id) {
//        return projectStageService.del(id);
//    }
//
//    @ApiOperation(value = "编辑")
//    @PostMapping("/edit")
//    public AjaxResult edit(@Valid @RequestBody ProjectStage dto) {
//        LoginUser loginUser = getLoginUser();
//        return projectStageService.edit(dto, loginUser);
//    }


    /**
     * 任务模块
     */
    @ApiOperation(value = "任务-明细")
    @GetMapping("/mission/info")
    public AjaxResult missionInfo(String id) {
        String userId = String.valueOf(getUserId());
        return stageMissionService.infoById(id, userId);
    }
    @ApiOperation(value = "任务-明细前台")
    @GetMapping("/mission/brief")
    public AjaxResult brief(String id) {
        LoginUser proUser = getProUser();
        return stageMissionService.briefById(id, proUser.getUserId());
    }

    private static LoginUser getProUser() {
        LoginUser loginUser = new LoginUser();
        //看情况是否需要优化--获取登录信息问题
        try {
            loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {

        }
        return loginUser;
    }
    @ApiOperation(value = "任务-删除")
    @PostMapping("/mission/del")
    @PreAuthorize("@ss.hasPermi('project:mission:del')")
    public AjaxResult missionDel(@RequestBody String[] ids) {
        LoginUser loginUser = getLoginUser();
        List<ProjectStageMission> projects = Arrays.stream(ids)
                .map(id -> {
                    ProjectStageMission mission = new ProjectStageMission();
                    mission.setId(id);
                    mission.setDelFlag("2");
                    return mission;
                })
                .collect(Collectors.toList());
        stageMissionService.updateBatchById(projects);

        return planService.del(ids) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @ApiOperation(value = "任务-编辑")
    @PostMapping("/mission/edit")
    @PreAuthorize("@ss.hasPermi('project:mission:edit')")
    public AjaxResult missionEdit(@Valid @RequestBody StageMissionDTO dto) throws JsonProcessingException {
        LoginUser loginUser = getLoginUser();
        return stageMissionService.edit(dto, loginUser);
    }

    @ApiOperation(value = "任务列表")
    @PostMapping("/mission/list")
    public TableDataInfo list(@Valid @RequestBody ProjectResultDTO dto) {
        LoginUser loginUser = getLoginUser();
        return stageMissionService.listByProId(dto, loginUser);
    }
    @ApiOperation(value = "任务列表（后台）")
    @PostMapping("/mission/adminList")
    public TableDataInfo adminList(@Valid @RequestBody ProjectResultDTO dto) {
        LoginUser loginUser = getLoginUser();
        return stageMissionService.adminListByProId(dto, loginUser);
    }
}
